<template>
	<view class="container">
		<view class="list-item"  @tap="select(index)" v-for="(item, index) in this.userSite" :key="index" >
			<view class="iconfont iconduihao" v-if="current==index"></view> <!--要想读到store里的东西有时候前面不能加this -->
			<view class="zhanwei" v-else></view>
			<view class="item" >
				<view class="top">
					<view class="name">{{ item.name }}</view>
					<view class="phone">{{ item.phone }}</view>
					<view class="tag">
						<text v-if="item.isdefault==1"  :class="{red:true}">{{'默认'}}</text>
					</view>
				</view>
				<view class="bottom" >
					{{item.pcd + ' '+ item.address}}
					<!-- <button class="mini-btn" type="warn" size="mini" @click.stop="delete_site(item.sid,index)">删除</button> -->
				</view>
			</view>
			<view @tap.stop="edit(item, index)">
				<u-icon name="edit-pen" :size="40" color="#999999"></u-icon>
			</view>
		</view>
		<view class="tianchong"></view>
		<view class="addSite" @tap="toAddSite">
			<view class="add">
				<u-icon name="plus" color="#ffffff" class="icon" :size="30"></u-icon>新建收货地址
			</view>
		</view>
	</view>
</template>

<script>
	import {mapState,mapGetters} from 'vuex'
	export default {
		data() {
			return {
				
			};
		},
		onLoad() {
			// this.select(-1)
		},
		computed:{
			...mapState(['userSite','current','userInformation']),
			...mapGetters(['show_userSite'])
		},
		methods: {
			select(i){
				console.log('select')
				this.$store.state.current = i
				// console.log('show_userSite',this.$store.getters.show_userSite)
			},
			
			//删除地址
			delete_site(sid,i){
				uni.showModal({
					title: '提示',
					content: '您确定要删除吗',
					success: (res) => {
						if(res.confirm){
							console.log('用户点击确定');
							this.$request({
								url:'/supermarket/userlogin/deleteaddress/'+sid
							}).then(()=>{
								this.$store.commit('change_current',i)
								this.$store.dispatch('get_userSite',this.userInformation.userID)
							})
						}
					}
				})
			},
			
			toAddSite(){
				uni.navigateTo({
					url: '/pages/addSite/addSite'
				});
			},
			
			edit(obj, index){
				console.log('edit',obj)
				uni.navigateTo({
					url:'/pages/editSite/editSite?info='+JSON.stringify(obj)+ '&index=' + index
					// url:'/pages/editSite/editSite?info='+JSON.stringify(obj)+'?index=' + index 
				})
			}
			
		}
	};
</script>

<style lang="scss" scoped>
	.tianchong{
		height: 120px;
	}
	.list-item{
		display: flex;
		align-items: center;
		padding-left: 15rpx;
		padding-right: 25rpx;
	}
	.iconduihao{
		color: #02a774;
		width: 32rpx;
	}
	.zhanwei{
		width: 32rpx;
	}
.item {
	padding: 40rpx 20rpx;
	border-bottom: 1rpx solid #ddd;
	flex: 1;
	.top {
		display: flex;
		font-weight: bold;
		font-size: 34rpx;
		.phone {
			margin-left: 60rpx;
		}
		.tag {
			display: flex;
			font-weight: normal;
			align-items: center;
			text {
				display: block;
				width: 60rpx;
				height: 34rpx;
				line-height: 34rpx;
				color: #ffffff;
				font-size: 20rpx;
				border-radius: 6rpx;
				text-align: center;
				margin-left: 30rpx;
				background-color:rgb(49, 145, 253);
			}
			.red{
				background-color:red
			}
		}
	}
	.bottom {
		display: flex;
		margin-top: 20rpx;
		font-size: 28rpx;
		justify-content: space-between;
		color: #999999;
	}
}
.addSite {
	display: flex;
	justify-content: space-around;
	width: 600rpx;
	line-height: 100rpx;
	position: fixed;
	bottom: 20rpx;
	left: 80rpx;
	background-color: #02a774;
	border-radius: 60rpx;
	font-size: 30rpx;
	.add{
		display: flex;
		align-items: center;
		color: #ffffff;
		.icon{
			margin-right: 10rpx;
		}
	}
}
</style>
